//lists.js
//获取应用实例
var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    newsList: [],
    pageNum:1,
    toastHidden:true,
    confirmHidden:true,
    isfrist:1,
    loadHidden:true,
    moreHidden:'none',
    msg:'没有更多文章了'
  },

  loadData: function (pageNum){
      //显示出加载中的提示
      this.setData({loadHidden:false})
      var that = this
      var data = {
        crcpnUser: wx.getStorageSync("userUuid"),
        crcpnUseStatus: 1,
        pageNum: pageNum,
        pageSize: 5
      };
      util.requestGet("coreCoupon/find/my/page", data, function (res) {
        if (!res.data) {
          that.setData({ toastHidden: false })
          that.setData({ moreHidden: 'none' })
          return false
        }
        var dataArr = that.data.newsList;
        console.log(res.data)
        var newData = dataArr.concat(res.data.result);
        if (pageNum == 1) {
          wx.setStorageSync('CmsList', newData)
        }
        that.setData({ pageNum: pageNum + 1})
        that.setData({ newsList: newData })
        that.setData({ moreHidden: '' })
        console.log('data from url');
        //显示出加载中的提示
        that.setData({ loadHidden: true }) 
      }, function (errData) {
        if (pageNum == 1) {
          var newData = wx.getStorageSync('CmsList')
          if (!newData) {
            that.setData({ newsList: newData })
            that.setData({ moreHidden: '' })
            that.setData({ pageNum: pageNum + 1 })
          }
          console.log('data from cache');
        } else {
          that.setData({ toastHidden: false, moreHidden: 'none', msg: '当前网格异常，请稍后再试' });
          //显示出加载中的提示
          that.setData({ loadHidden: true }) 
        }
      });
  },
  loadMore: function(event){
    var pageNum = event.currentTarget.dataset.pagenum
    var isfrist = event.currentTarget.dataset.isfrist
    var that = this

    wx.getNetworkType({
      success: function(res) {
        var networkType = res.networkType // 返回网络类型2g，3g，4g，wifi

        if(networkType!='wifi' && isfrist=='1'){
           that.setData({confirmHidden:false})
        }
      }
    })

    this.setData({isfrist:0})
    this.loadData(pageNum);
  },
  onLoad: function () {
    var that = this;
    this.loadData(1);
  },
  toastChange: function(){
    this.setData({toastHidden:true})
  },
  modalChange: function(){
    console.log('abc');
    this.setData({confirmHidden:true})
  }
})